package com.yixin.hospital.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yixin.hospital.entity.domain.AuditFlowPreset;
import com.yixin.hospital.entity.domain.JobDuty;
import com.yixin.hospital.entity.domain.User;
import com.yixin.hospital.entity.dto.req.auditflowpreset.AuditFlowPresetAddReqDTO;
import com.yixin.hospital.entity.dto.req.auditflowpreset.AuditFlowPresetEditReqDTO;
import com.yixin.hospital.entity.vo.AuditFlowPresetVO;
import com.yixin.hospital.entity.vo.JobDutyVO;
import com.yixin.hospital.enums.BusinessEnum;
import com.yixin.hospital.service.AuditFlowPresetService;
import com.yixin.hospital.mapper.AuditFlowPresetMapper;
import com.yixin.hospital.service.DeptService;
import com.yixin.hospital.service.JobDutyService;
import com.yixin.hospital.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* @author Administrator
* @description 针对表【audit_flow_preset(审批流预设表)】的数据库操作Service实现
* @createDate 2024-06-07 17:55:07
*/
@Service
public class AuditFlowPresetServiceImpl extends ServiceImpl<AuditFlowPresetMapper, AuditFlowPreset>
    implements AuditFlowPresetService{

    @Autowired
    private JobDutyService jobDutyService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private UserService userService;


    @Override
    public List<AuditFlowPresetVO> allList() {
        List<AuditFlowPresetVO> resp = new ArrayList<>();
        for (AuditFlowPreset auditFlowPreset : this.list()) {
            AuditFlowPresetVO vo = new AuditFlowPresetVO();
            BeanUtil.copyProperties(auditFlowPreset, vo, "jobDuty");
            vo.setBusinessName(BusinessEnum.getDesc(auditFlowPreset.getBusinessCode()));
            List<JobDutyVO> jobDutyList = new ArrayList<>();
            String[] split = auditFlowPreset.getJobDuty().split(",");
            for (String jobDutyId : split) {
                JobDuty jobDuty = jobDutyService.getById(Long.valueOf(jobDutyId));
                JobDutyVO jobDutyVO = new JobDutyVO();
                BeanUtils.copyProperties(jobDuty, jobDutyVO);
                jobDutyVO.setDeptName(deptService.getById(jobDuty.getDeptId()).getName());
                jobDutyList.add(jobDutyVO);
            }
            vo.setJobDutyList(jobDutyList);
            resp.add(vo);
        }
        return resp;
    }

    @Override
    public List<JobDutyVO> getInfo(String businessCode) {
        LambdaUpdateWrapper<AuditFlowPreset> wrapper = new LambdaUpdateWrapper<>();
        if (StringUtils.isNotBlank(businessCode)) wrapper.eq(AuditFlowPreset::getBusinessCode, businessCode);
        AuditFlowPreset auditFlowPreset = this.getOne(wrapper);
        List<JobDutyVO> jobDutyList = new ArrayList<>();
        String[] split = auditFlowPreset.getJobDuty().split(",");
        for (String jobDutyId : split) {
            JobDutyVO jobDutyVO = new JobDutyVO();
            JobDuty jobDuty = jobDutyService.getById(Long.valueOf(jobDutyId));
            BeanUtils.copyProperties(jobDuty, jobDutyVO);
            List<User> userListByJobDuty = userService.list(new LambdaQueryWrapper<User>().like(User::getJobDuty, jobDuty.getId()));
            userService.list().stream().filter(user -> {
                String[] jobDutyListByUser = user.getJobDuty().split(",");
                for (String s : jobDutyListByUser) {
                    if (Objects.equals(s, jobDuty.getId())) return true;
                }
                return false;
            });
            jobDutyVO.setAuditAvailableUserList(userListByJobDuty);
            jobDutyList.add(jobDutyVO);
        }
        return jobDutyList;
    }

    @Override
    public void add(AuditFlowPresetAddReqDTO auditFlowPresetAddReqDTO, String userName) {
        AuditFlowPreset auditFlowPreset = new AuditFlowPreset();
        auditFlowPreset.setBusinessCode(auditFlowPresetAddReqDTO.getBusinessCode());
        auditFlowPreset.setJobDuty(auditFlowPresetAddReqDTO.getJobDutyList());
        auditFlowPreset.setCreatedBy(userName);
        auditFlowPreset.setUpdatedBy(userName);
        this.save(auditFlowPreset);
    }

    @Override
    public void edit(AuditFlowPresetEditReqDTO auditFlowPresetEditReqDTO, String userName) {
        AuditFlowPreset auditFlowPreset = new AuditFlowPreset();
        auditFlowPreset.setId(auditFlowPresetEditReqDTO.getId());
        auditFlowPreset.setJobDuty(auditFlowPresetEditReqDTO.getJobDutyList());
        auditFlowPreset.setUpdatedBy(userName);
        this.updateById(auditFlowPreset);
    }
}




